/**
 * Created by wangbin on 2017/11/22.
 */
$(function() {
    var url = {
        listUndo: ctx+"/costAcct/listUndoList",
        genCheckRecord:ctx+"/costAcct/updateItemStatus",
        cancelRecord: ctx+"/costAcct/updateItemStatus"
    };
    var initQuery = function () {
        qryLeft();
        qryCheckDetail(null);
    };
    var qryLeft = function () {
        var param = $("#qry-undo-form-one").serialize();
        $(top.callService("post",url.listUndo,param,function (data) {
            $("#simple-table-one tbody").html(data);
        },null,"html"));
    };
    var qryCheckDetail = function (checkId) {
        var param = $("#qry-done-form-one").serialize();
       /* var t = $("#simple-table-cost-one").find("input[name='ids']:checked").first();
        param.checkId = checkId || t.val() || "";
        if (param.checkId.length<1){
            return;
        }*/
        $(top.callService("post",url.listUndo,param,function (data) {
            $("#check-cost-detail-one tbody").html(data);
        },null, "html"));
    };

    var init = function() {
        //XCF.listener();
        $(".js_selectAll").click(function (evt) {
            var t = $(evt.target).attr("checked");
            var items = $(evt.target).parent().parent().parent().parent().
            parent().find("input[type='checkbox']");
            if (t =="checked"){
                $.each(items,function (idx,item) {
                    $(item).attr("checked",t);
                });
            }else {
                $.each(items,function (idx,item) {
                    $(item).removeAttr("checked");
                });
            }
        });
        $(".js_btn_gen-one").click(function () {
            var ids = "";
            var dep = "";
            $.each($("#simple-table-one").find("input[name='ids']:checked"),function (idx,item) {
                ids = ids.concat(",").concat(item.id);
                if (idx<1){
                    dep = item.alt;
                }
            });
            if (ids.length<1){
                alert("请选择未对账明细");
                return;
            }
            var param = {};
            param.depart = dep;
            param.undoList = ids.substring(1);
            param.status="2";
            param.dataType = "prepare";
            $(top.callService("post",url.genCheckRecord,param,function(data){
                initQuery();
            }));
        });

        $(".js_btn_invalid_items-one").click(function () {
            var ids = "";
            var checkId= "";
            $.each($("#check-cost-detail-one").find("input[name='ids']:checked"),
                function (idx,item) {
                    ids = ids.concat(",").concat(item.id);
                    checkId = item.alt;
                });
            if(ids.length<1){
                alert("选择要操作的数据！")
                return;
            }
            var param = {};
            param.checkId = checkId;//$("#checkId").val();
            param.undoList = ids.substring(1);
            param.status="0";
            param.dataType = "prepare";
            $(top.callService("post",url.cancelRecord,param,function(data){
                initQuery();
            }));
        });

        $(".div-left-search-one").css("display","none");
        /*列表查询*/
        $(".js_btn_left_search-one").click(function () {
            $(".div-left-search-one").css("display","block");
        }) ;
        $(".js_left_query-one").click(function () {
            $(".div-left-search-one").css("display","none");
            qryLeft();
        });
        $(".js_btn_qry_check_detail-one").click(function () {
            qryCheckDetail();
        });

        initQuery();
    };
    init();
});